import moment from 'moment';
import api from '@/api';
import DevMock from '@/config/devMock';
import { TRADER, AUDITOR, OPERATOR, getRoleNameByRoleID, getRolesNameByRolesID, getDefaultRoleByRolesName } from '@/config/permission';

const permission = {
  namespaced: true,
  state: {
    roles: [],
    rolesName: [], // ['TRADER', 'AUDITOR', 'OPERATOR']
    routers: {
      [TRADER]: [],
      [AUDITOR]: [],
      [OPERATOR]: []
    },
    // 当前角色 (页面导航栏顶部可切换当前角色)
    currentRole: '', // 'TRADER', 'AUDITOR', 'OPERATOR'
    endDateInfo: [],
    operatorInvestType: [],
    traderFundType: []
  },
  mutations: {
    SET_ROLES: (state, payload) => {
      state.roles = payload;
    },
    SET_ROLES_NAME: (state, payload) => {
      state.rolesName = payload;
    },
    SET_ROUTERS_TRADER: (state, payload) => {
      state.routers[TRADER] = payload;
    },
    SET_ROUTERS_AUDITOR: (state, payload) => {
      state.routers[AUDITOR] = payload;
    },
    SET_ROUTERS_OPERATOR: (state, payload) => {
      state.routers[OPERATOR] = payload;
    },
    SET_CURRENT_ROLE (state, payload) {
      state.currentRole = payload;
      localStorage.setItem('currentRole', payload);
    },
    SET_ENDDATE_INFO (state, payload) {
      state.endDateInfo = payload;
    },
    SET_INVEST_TYPE (state, payload) {
      state.operatorInvestType = payload;
    },
    SET_FUND_TYPE (state, payload) {
      state.traderFundType = payload;
    }
  },
  actions: {
    get ({ commit }) {
      return new Promise((resolve, reject) => {
        api.permission.getPermission().then(res => {
          const data = res.data || {};
          const {
            roles = '',
            traderRouters = '',
            auditorRouters = '',
            operRouters = '',
            endDateInfo = [],
            totalInvestType = '',
            totalFundType = ''
          } = data;
          const rolesArr = roles.split('|');
          const traderRoutersArr = traderRouters.split('|');
          const auditorRoutersArr = auditorRouters.split('|');
          const operRoutersArr = operRouters.split('|');
          const totalInvestTypeArr = totalInvestType.split('|');
          const totalFundTypeArr = totalFundType.split('|');
          let rolesName = getRolesNameByRolesID(rolesArr);
          const { FAKE_ROLE } = DevMock;
          if (process.env.NODE_ENV === 'development' && FAKE_ROLE) {
            rolesName = FAKE_ROLE;
          }
          // 获取当前角色
          let currentRole = '';
          const storageCurrentRole = localStorage.getItem('currentRole');
          const defaultCurrentRole = getDefaultRoleByRolesName(rolesName);
          if (rolesName.includes(storageCurrentRole)) {
            currentRole = storageCurrentRole;
          } else {
            currentRole = defaultCurrentRole;
          }
          // 到期日处理
          const endDate = {};
          endDateInfo.forEach(item => {
            const roleName = getRoleNameByRoleID(item.roleId);
            endDate[roleName] = moment(item.endDate, 'YYYYMMDD').format('YYYY-MM-DD');
          });

          commit('SET_ROLES', rolesArr);
          commit('SET_ROLES_NAME', rolesName);
          commit('SET_ROUTERS_TRADER', traderRoutersArr);
          commit('SET_ROUTERS_AUDITOR', auditorRoutersArr);
          commit('SET_ROUTERS_OPERATOR', operRoutersArr);
          commit('SET_CURRENT_ROLE', currentRole);
          commit('SET_ENDDATE_INFO', endDate);
          commit('SET_INVEST_TYPE', totalInvestTypeArr);
          commit('SET_FUND_TYPE', totalFundTypeArr);

          resolve(rolesName);
        }).catch(error => reject(error));
      });
    }
  }
};

export default permission;
